package personal.wang.juc;

import javax.swing.plaf.synth.SynthTextAreaUI;
import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

public class CompletableFutureTest3 {
    public static void main(String[] args) throws IOException {
        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> 1);

        CompletableFuture<Void> voidCompletableFuture = integerCompletableFuture.thenCompose((input) -> {
            return CompletableFuture.completedFuture(input + 1);
        }).thenApply((x) -> {
            int a = 1/0;
            return x + 1;
        }).thenAccept((x) -> {
            System.out.println(x);
        }).exceptionally((throwable -> {
            System.out.println("exceptionally:" + throwable.getMessage()) ;
            return null ;
        })).whenComplete((record , throwable)->{
            System.out.println("throwable" + throwable);
               if(Objects.nonNull(throwable)) {
                   System.out.println("whenComplete: " + throwable.getMessage());
               }
        });

        CompletableFuture<Integer> result = voidCompletableFuture.thenAcceptAsync(x -> {
            int a = 1/0;
            System.out.println("1111:" + x);
        } ,Executors.newSingleThreadExecutor(new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, "mythread");
            }
        })).handle((record , throwable) -> {
            if(Objects.nonNull(throwable)){
                System.out.println(throwable.getMessage());
            }
            return 1 ;
        });
        System.out.println(result.join());

    }
}
